/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.util;

/**
 * A basic simple validator to validate form inputs. It also generates the error
 * messages for display purpose.
 * 
 * @author Wenbin
 * 
 */
public class Validator {
	private String _tag;
	private String _errorMessages="";
	private static final int HOUR_IN_MILLIS=3600000;
	private static final int MIN_IN_MILLIS=60000;
	
	public static Validator newInstance(String tag) {
		return new Validator(tag);
	}

	private Validator(String tag) {
		this.set_tag(tag);
	}
	
	/**
	 * Check to see if the input string is null or empty. 
	 * @param source
	 * @param message
	 * @return
	 */
	public boolean checkNull(String source, String message) {
		boolean result=false;
		if (source!=null) {
			result=source.trim().length()>0;
		}
		setResult(result, message);
		return result;
	}
	
	/**
	 * Check the result and set the error message based on it.
	 * @param result
	 * @param message
	 */
	private void setResult(boolean result, String message) {
		if (!result) {
			addErrorMessages(message);
		}
	}
	
	/**
	 * A utility method to check null String input without generating an error
	 * message.
	 * 
	 * @param source
	 * @return
	 */
	public static boolean checkNonRequiredNull(String source){
		boolean result=false;
		if (source!=null) {
			result=source.trim().length()>0;
		}
		return result;
	}
	
	/**
	 * Check to see if num1 is <= num2.
	 * @param num1
	 * @param num2
	 * @param message
	 * @return
	 */
	public boolean checkDecimalMinMax(double num1,double num2,String message){
		boolean result=false;
		result=checkDecimalMinMax(num1, num2);
		setResult(result, message);
		return result;
	}
	
	public static boolean checkDecimalMinMax(double num1,double num2){
		boolean result=false;
		if (num1!=num2) {
			if (Math.max(num1, num2)!=num2) {
				result=false;
			}else {
				result=true;
			}
		}else {
			result=true;
		}
		return result;
	}
	
	/**
	 * Check zero number.
	 * @param num
	 * @param message
	 * @return
	 */
	public boolean checkZeroNum(double num, String message){
		boolean result=false;
		if (num<=0) {
			result=false;
		}else {
			result=true;
		}
		setResult(result, message);
		return result;
	}
	
	/**
	 * Check to see if the time interval between time1 and time2 is within the
	 * bound of the given hours and minutes.
	 * 
	 * @param time1
	 * @param time2
	 * @param atLeastHours
	 * @param atLeastMins
	 * @param message
	 * @return
	 */
	public boolean checkTimeInterval(long time1, long time2, int atLeastHours,int atLeastMins,String message){
		boolean result=false;
		
		if (time1<time2) {
			long interval=atLeastHours*HOUR_IN_MILLIS+atLeastMins*MIN_IN_MILLIS;
			if (time2-time1>=interval) {
				result=true;
			}else {
				result=false;
			}
		}else {
			if (time1==time2&&atLeastHours==0&&atLeastMins==0) {
				result=true;
			}else {
				result=false;
			}
		}
		setResult(result, message);
		return result;
	}
	
	private void addErrorMessages(String msg){
		_errorMessages+=" - "+msg+"\n";
	}

	public void reset() {
		_errorMessages="";
	}
	
	public String get_tag() {
		return _tag;
	}

	public void set_tag(String _tag) {
		this._tag = _tag;
	}

	public String get_errorMessages() {
		if (_errorMessages.trim().length()==0) {
			return null;
		}
		return "Please Fix the following error(s):\n"+_errorMessages;
	}

}
